package com.pro.springbootshop.controller;

import com.pro.springbootshop.entity.Cart;
import com.pro.springbootshop.service.CartService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CartController {
    @Autowired
    public CartService cartService;
    @ApiOperation("添加商品到购物车或修改购物车中商品的数量")
    @PostMapping("/api/cart/{waresId}/{waresNumber}")
    public String addOrUpdateCart(@PathVariable(name = "waresId") Integer waresId,@PathVariable(name = "waresNumber") Integer waresNumber,HttpServletRequest request){
        String token = request.getHeader("token");
        return cartService.addOrUpdateCart(waresId,waresNumber,token);
    }
    @ApiOperation("获取购物车中所有商品信息")
    @GetMapping("/api/cart/cartList")
    public List<Cart> getCartList(HttpServletRequest request){
        String token = request.getHeader("token");
        return cartService.getCartList(token);
    }
    @ApiOperation("删除购物车中商品")
    @GetMapping("/api/cart/{cartId}")
    public String deleteCart(@PathVariable(name = "cartId") Integer cartId){
        return cartService.deleteCart(cartId);
    }
    @ApiOperation("修改购物车中商品选中状态")
    @GetMapping("/api/cart/checkCart/{waresId}/{cartState}")
    public String updateCartChecked(@PathVariable(name = "waresId") Integer waresId,@PathVariable(name = "cartState") Integer cartState,HttpServletRequest request){
        String token = request.getHeader("token");
        return cartService.updateCartChecked(waresId,cartState,token);
    }
}
